<script setup lang="ts">
import { useUserStore } from '@/stores/user'
import { useAuth } from '@/composables/auth'
import { useToastStore } from '@/stores/toast'

// 获取用户状态存储
const userStore = useUserStore()
const { logout } = useAuth()
const toastStore = useToastStore()

// 处理登出
const handleLogout = async () => {
  try {
    await logout()
    userStore.clearUserInfo()
    toastStore.addToast('已安全退出', 'warning')
  } catch (e) {
    toastStore.addToast('退出失败，请重试', 'error')
  }
}
</script>

<template>
  <div class="main-layout">
    <header class="nes-container">
      <div class="header-left">
        <i class="nes-icon coin is-medium"></i>
      </div>
      
      <h1 class="site-title">数字操作游戏</h1>
      
      <div v-if="userStore.isAuthenticated" class="user-info">
        <span class="username">
          <i class="nes-icon trophy is-small"></i>
          {{ userStore.username }}
        </span>
        <button 
          @click="handleLogout" 
          class="nes-btn is-error"
        >
          退出
        </button>
      </div>
      <div v-else class="header-right"></div>
    </header>
    
    <main class="main-content">
      <slot />
    </main>
  </div>
</template>

<style scoped>
.main-layout {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-color);
}

.header {
  height: 64px;
  padding: 0 2rem;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0;
  border-image-repeat: stretch;
}

.header-left, .header-right {
  width: 100px;
  display: flex;
  align-items: center;
}

.site-title {
  margin: 0;
  font-size: 1rem;
  text-align: center;
  white-space: nowrap;
}

.main-content {
  flex: 1;
  padding: 1rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: 0.7rem;
  justify-self: end;
}

.username {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.nes-btn.is-error {
  margin: 0;
  padding: 0.25rem 0.5rem;
  font-size: 0.7rem;
  min-width: auto;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .header {
    padding: 0 1rem;
  }

  .site-title {
    font-size: 0.8rem;
  }

  .username {
    display: none;
  }
}
</style> 